﻿package mortal.game.control.subControl
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.mouse.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.processor.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.item.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.petBreak.*;
    import mortal.game.view.copy.sunbathCopy.*;
    import mortal.game.view.copy.wine.util.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.task.drama.*;
    import mortal.game.view.test.*;
    import mortal.mvc.core.*;

    public class Scene3DClickProcessor extends Object
    {
        private static var _scene:GameScene3D;
        private static var _lastClickScene:int = 0;
        private static var _lastClickEntity:int = 0;
        private static var _params:Array = [];
        private static var _instance:Scene3DClickProcessor;
        private static var showpath1:Array;
        private static var showpath2:Array;
        private static var showpath3:Array;
        private static var showpath4:Array;
        private static var _findPathCallback:Function;

        public function Scene3DClickProcessor(param1:GameScene3D)
        {
            _scene = param1;
            return;
        }// end function

        public function work(event:MouseEvent) : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:Point = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            if (TaskDramaOperator.isInFlyDrama)
            {
                return;
            }
            if (!Game.isSceneInit())
            {
                return;
            }
            if (!GameController.npc.isTaskDialogHide())
            {
                if (GameController.npc.npcDialog.clickStageHandler2(event))
                {
                    return;
                }
            }
            if (event.target == Global.stage && !LayerManager.isHasFullSceenOpen())
            {
                if (GuideAutoFightWin.hasInstance && !GuideAutoFightWin.instance.isHide)
                {
                    GuideAutoFightWin.instance.hide();
                }
                _loc_2 = event.type == MouseEvent.MOUSE_DOWN && (!SystemSetting.instance.mouseRightSceneKey.bValue || !FlashVersion.instance.isVersionMeet(11, 2)) || event.type == "rightMouseDown" && SystemSetting.instance.mouseRightSceneKey.bValue;
                _loc_3 = Scene3DUtil.getSceneMousePostion(event.stageX, event.stageY, true);
                if (_loc_2 && event.type == MouseEvent.MOUSE_DOWN)
                {
                    MouseLongPress.instance.setMouseDown();
                }
                if (ThingUtil.overEntity)
                {
                    if (ThingUtil.overEntity is IEntity)
                    {
                        ThingUtil.selectEntity = ThingUtil.overEntity as IEntity;
                    }
                    if (!BuffUtil.isCanRoleWalk())
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20375));
                        return;
                    }
                    if (SunbathUtil.isInPreform() && !(ThingUtil.overEntity is UserPlayer))
                    {
                        SunbathUtil.showQuickPerformAlert(sceneClickEntity, [ThingUtil.overEntity, event.clone()]);
                        return;
                    }
                    sceneClickEntity(ThingUtil.overEntity, event);
                }
                else
                {
                    if (!_loc_2)
                    {
                        return;
                    }
                    _loc_4 = getTimer();
                    _loc_5 = _lastClickScene;
                    if (_loc_4 - _lastClickScene < 200)
                    {
                        return;
                    }
                    _lastClickScene = _loc_4;
                    if (!BuffUtil.isCanRoleWalk())
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20375));
                        return;
                    }
                    if (GameController.copy.winePartyCopy.isStartMora)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(20376));
                        return;
                    }
                    if (GameMapUtil.curMapState.isPetBreakMap)
                    {
                        AIManager.cancelAll();
                        selfPetGotoPoint(_loc_3);
                    }
                    else if (SunbathUtil.isInPreform())
                    {
                        SunbathUtil.showQuickPerformAlert(gotoPoint, [_loc_3.clone(), true, 0]);
                    }
                    else if (!Cache.instance.role.roleEntityInfo.isDead && !RolePlayer.instance.isJumping)
                    {
                        if (AIManager.singleClickNoStop && _loc_4 - _loc_5 > 1000)
                        {
                            MsgManager.showRollTipsMsg(Language.getString(20377));
                            return;
                        }
                        AIManager.cancelAll(false);
                        GuideEffect.isShowPath = false;
                        AIManager.singleClickNoStop = false;
                        gotoPoint(_loc_3, true, 0);
                    }
                }
            }
            return;
        }// end function

        public static function get instance() : Scene3DClickProcessor
        {
            if (!_instance)
            {
                _instance = new Scene3DClickProcessor(Game.scene);
            }
            return _instance;
        }// end function

        private static function clearEvent() : void
        {
            FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, RolePlayerEvent.SpasticityClear, sceneClickEntityImpl);
            FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, RolePlayerEvent.SpasticityClear, gotoPointImpl);
            return;
        }// end function

        private static function sceneClickEntity(param1:Game2DPlayer, param2:MouseEvent) : void
        {
            _params[0] = param1;
            _params[1] = param2;
            if (RolePlayer.instance.isJumping)
            {
                return;
            }
            if (RolePlayer.instance.isInSpasticity)
            {
                clearEvent();
                FrEventDispatcher.instance.proxyAddEventListener(RolePlayer.instance, RolePlayerEvent.SpasticityClear, sceneClickEntityImpl);
            }
            else
            {
                sceneClickEntityImpl();
            }
            return;
        }// end function

        private static function sceneClickEntityImpl(param1 = null) : void
        {
            var _loc_4:SPassPoint = null;
            var _loc_5:Point = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:IEntity = null;
            var _loc_9:Boolean = false;
            var _loc_10:Boolean = false;
            var _loc_11:SkillInfo = null;
            if (RolePlayer.instance.isJumping)
            {
                return;
            }
            FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, RolePlayerEvent.SpasticityClear, sceneClickEntityImpl);
            if (!(_params[0] is Game2DPlayer))
            {
                return;
            }
            var _loc_2:* = _params[0];
            var _loc_3:* = _params[1];
            if (_loc_2 is PassPlayer)
            {
                _loc_4 = PassPlayer(_loc_2).passPoint;
                AIManager.onAIControl(AIType.GoToAndPass, _loc_4);
            }
            else if (_loc_2 is JumpPlayer)
            {
                _loc_5 = new Point();
                _loc_5.x = JumpPlayer(_loc_2).spoint.x;
                _loc_5.y = JumpPlayer(_loc_2).spoint.y;
                AIManager.onAIControl(AIType.GoToAndJump, _loc_5);
            }
            else if (_loc_2 is ItemPlayer)
            {
                if ((_loc_2 as ItemPlayer).dropEntityInfo)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PickDropItem, (_loc_2 as ItemPlayer).dropEntityInfo.entityId));
                }
            }
            else if (_loc_2 is StatuePlayer)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ClickCityKing));
            }
            else
            {
                _loc_6 = FrameManager.flashFrame.getTime();
                _loc_7 = _lastClickEntity;
                _lastClickEntity = _loc_6;
                if (_loc_2 is IEntity)
                {
                    _loc_8 = _loc_2 as IEntity;
                }
                if (_loc_8 && !Cache.instance.role.roleEntityInfo.isDead && !RolePlayer.instance.isJumping)
                {
                    _loc_9 = SystemSetting.instance.isSelectFightToDead.bValue && _loc_3.type == MouseEvent.MOUSE_DOWN;
                    if (_loc_8 is NPCPlayer)
                    {
                        if (AIManager.curStopabele)
                        {
                            AIManager.onAIControl(AIType.ClickNpc, NPCPlayer(_loc_8).npcInfo.tnpc.code, true);
                        }
                    }
                    else if (_loc_8 is MonsterPlayer && BossRule.isCollectBoss(MonsterPlayer(_loc_8)))
                    {
                        if (WinePartyUtil.isWineHasCollected(_loc_8 as MonsterPlayer))
                        {
                            return;
                        }
                        if (GameMapUtil.curMapState.isGuildWar && (_loc_8 as MonsterPlayer).entityInfo.entityInfo.force == Cache.instance.role.entityInfo.force)
                        {
                            return;
                        }
                        if (CollectAI.entityId != null && EntityUtil.equal(CollectAI.entityId, MonsterPlayer(_loc_8).entityInfo.entityInfo.entityId))
                        {
                            return;
                        }
                        if (_loc_8.entityID == Cache.instance.skill.waittingServerCollectID)
                        {
                            return;
                        }
                        if (AIManager.curAI != null && AIManager.curAI is CollectAI && AIManager.curAI.data.target == _loc_8)
                        {
                            return;
                        }
                        if (AIManager.singleClickNoStop && _loc_6 - _loc_7 > 1000)
                        {
                            MsgManager.showRollTipsMsg(Language.getString(20378));
                            return;
                        }
                        AIManager.onAIControl(AIType.Collect, _loc_8);
                    }
                    else
                    {
                        if (!FightUtil.isCanAttackPlayer(true))
                        {
                            return;
                        }
                        _loc_10 = false;
                        if (ThingUtil.selectEntity != null)
                        {
                            _loc_10 = EntityDictionary.isEntityFit(ThingUtil.selectEntity, null, EntityRelationUtil.ENEMY, false, true);
                        }
                        if (_loc_8.entityInfo && !_loc_8.entityInfo.isDead && _loc_10)
                        {
                            _loc_11 = PetBreakCopyUtil.firstSkill;
                            if (_loc_11 == null)
                            {
                                return;
                            }
                            if (GameMapUtil.isCanNotPlayerSkillMap())
                            {
                                return;
                            }
                            if (AIManager.singleClickNoStop && _loc_6 - _loc_7 > 1000)
                            {
                                MsgManager.showRollTipsMsg(Language.getString(20379));
                                return;
                            }
                            ThingUtil.selectEntity = _loc_8;
                            if (_loc_9)
                            {
                                AIManager.onAIControl(AIType.FightBossToDead);
                            }
                            else
                            {
                                GameController.skill.checkAndStartAIBySkill(_loc_11, true);
                            }
                        }
                    }
                }
            }
            return;
        }// end function

        public static function selfPetGotoPoint(param1:Point) : void
        {
            var _loc_3:Array = null;
            var _loc_2:* = ThingUtil.entityUtil.getSelfPetEntity();
            if (_loc_2)
            {
                _scene.addPointMark(param1.x, param1.y);
                if (_loc_2 == null)
                {
                    return;
                }
                _loc_3 = findPath(_loc_2.x2d, _loc_2.y2d, param1.x, param1.y, true);
                _loc_2.walking(_loc_3[0]);
            }
            return;
        }// end function

        public static function gotoPoint(param1:Point, param2:Boolean = true, param3:int = -1, param4:Boolean = true) : void
        {
            if (param4)
            {
                _scene.addPointMark(param1.x, param1.y);
            }
            _params[0] = new Point(param1.x, param1.y);
            _params[1] = param2;
            _params[2] = param3;
            if (RolePlayer.instance.isInSpasticity)
            {
                clearEvent();
                FrEventDispatcher.instance.proxyAddEventListener(RolePlayer.instance, RolePlayerEvent.SpasticityClear, gotoPointImpl);
            }
            else
            {
                gotoPointImpl();
            }
            return;
        }// end function

        private static function gotoPointImpl(param1 = null) : void
        {
            if (RolePlayer.instance.isJumping)
            {
                return;
            }
            if (!BuffUtil.isCanRoleWalk())
            {
                return;
            }
            FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, RolePlayerEvent.SpasticityClear, gotoPointImpl);
            if (!(_params[0] is Point))
            {
                return;
            }
            var _loc_2:* = _params[0];
            var _loc_3:* = _params[1];
            var _loc_4:* = _params[2];
            var _loc_5:* = new Point(RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            if (_loc_3)
            {
                AIManager.cancelAll(false);
            }
            AIManager.addMoveTo(_loc_2, _loc_4);
            if (_loc_3)
            {
                AIManager.start();
            }
            return;
        }// end function

        public static function findPath(param1:int, param2:int, param3:int, param4:int, param5:Boolean) : Array
        {
            var _loc_6:* = getTimer();
            var _loc_7:* = findPathImpl(param1, param2, param3, param4, param5);
            if (getTimer() - _loc_6 > 100)
            {
                MoveUtil.setLastMoveTime();
            }
            return _loc_7;
        }// end function

        public static function findPathImpl(param1:int, param2:int, param3:int, param4:int, param5:Boolean) : Array
        {
            var _loc_9:Array = null;
            var _loc_10:Array = null;
            var _loc_11:AstarTurnPoint = null;
            var _loc_12:Array = null;
            var _loc_13:Number = NaN;
            var _loc_14:Point = null;
            var _loc_15:Array = null;
            var _loc_16:int = 0;
            var _loc_17:AstarTurnPoint = null;
            var _loc_18:Object = null;
            var _loc_19:Array = null;
            var _loc_20:int = 0;
            var _loc_21:int = 0;
            var _loc_22:int = 0;
            var _loc_23:AstarTurnPoint = null;
            var _loc_6:* = getTimer();
            var _loc_7:* = GameMapUtil.getTilePoint(param1, param2);
            var _loc_8:* = GameMapUtil.getTilePoint(param3, param4);
            if (!TestPanel.isPath)
            {
                _loc_9 = AstarAnyDirection.findPath(_loc_7.x, _loc_7.y, _loc_8.x, _loc_8.y, param5);
            }
            else
            {
                _loc_9 = [];
                _loc_10 = [];
                _loc_11 = new AstarTurnPoint();
                _loc_11._x = _loc_7.y;
                _loc_11._y = _loc_7.x;
                _loc_10.push(_loc_11);
                _loc_11 = new AstarTurnPoint();
                _loc_11._x = _loc_8.y;
                _loc_11._y = _loc_8.x;
                _loc_10.push(_loc_11);
                _loc_9.push(_loc_10);
            }
            if (_loc_9 == null || _loc_9.length == 0)
            {
                Log.system("找不到有效路径");
                return [];
            }
            _loc_12 = [];
            _loc_15 = [];
            _loc_16 = 0;
            while (_loc_16 < _loc_9.length)
            {
                
                _loc_18 = _loc_9[_loc_16];
                if (_loc_18 is Array)
                {
                    _loc_19 = _loc_18 as Array;
                    _loc_22 = 0;
                    while (_loc_22 < _loc_19.length)
                    {
                        
                        _loc_17 = _loc_19[_loc_22];
                        _loc_13 = _loc_17._x;
                        _loc_17._x = _loc_17._y;
                        _loc_17._y = _loc_13;
                        _loc_14 = new Point();
                        _loc_14.x = int(_loc_17._x);
                        _loc_14.y = int(_loc_17._y);
                        _loc_12.push(_loc_14);
                        if (_loc_22 == (_loc_19.length - 1))
                        {
                            _loc_20 = (_loc_17._x + 0.5) * GameMapUtil.tileWidth;
                            _loc_21 = (_loc_17._y + 0.5) * GameMapUtil.tileHeight;
                        }
                        _loc_22++;
                    }
                    _loc_19 = MapGridToPixels.work(_loc_19);
                    if (_loc_16 == (_loc_9.length - 1))
                    {
                        if (AstarAnyDirection.checkIsWalkable(_loc_8.x, _loc_8.y))
                        {
                            _loc_20 = param3;
                            _loc_21 = param4;
                        }
                        else
                        {
                            _loc_23 = _loc_19[(_loc_19.length - 1)] as AstarTurnPoint;
                            _loc_20 = _loc_23._x;
                            _loc_21 = _loc_23._y;
                        }
                    }
                    MapGridToTargetPoint.work(_loc_19, _loc_20, _loc_21);
                    _loc_9[_loc_16] = _loc_19;
                }
                else
                {
                    _loc_17 = _loc_18 as AstarTurnPoint;
                    _loc_13 = _loc_17._x;
                    _loc_17._x = _loc_17._y;
                    _loc_17._y = _loc_13;
                    _loc_14 = new Point();
                    _loc_14.x = int(_loc_17._x);
                    _loc_14.y = int(_loc_17._y);
                    _loc_12.push(_loc_14);
                }
                _loc_16++;
            }
            if (_loc_12.length > 0)
            {
                _loc_12.shift();
            }
            Cache.instance.smallmap.paths = _loc_12;
            return _loc_9;
        }// end function

    }
}
